package com.kindergarten.config;

import com.kindergarten.filter.CheckCodeFilter;
import com.kindergarten.handler.LoginFailureHandler;
import com.kindergarten.handler.LoginSuccessHandler;
import com.kindergarten.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * SpringSecurityConfig配置类，这个类相当于 spring-security.xml
 */
@Configuration
@EnableWebSecurity //注解是Spring Security用于启用web安全的注解
//secured-annotations="enabled"这才是SpringSecurity提供的注解
@EnableGlobalMethodSecurity(securedEnabled = true) //开启注解授权
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {


    /**
     * 认证成功的处理器
     */
    @Autowired
    LoginSuccessHandler loginSuccessHandler;

    /**
     * 认证失败处理器
     */
    @Autowired
    LoginFailureHandler loginFailureHandler;

    @Autowired
    IUserService userService;

    /**
     * 验证码校验过滤器
     */
    @Autowired
    CheckCodeFilter checkCodeFilter;

    /**
     * 先通过内存中的账号密码来处理-认证数据源
     *
     * @param auth
     * @throws Exception
     */
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        //        auth.inMemoryAuthentication()
        //                .withUser("admin")
        //                .password("{noop}123")
        //                .roles("USER");
        auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
    }

    /**
     * 自定义配置
     * ​安全规则
     *
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        //支持项目中的框架frame布局
        http.headers().frameOptions().sameOrigin();
        http.authorizeRequests()
                .antMatchers("/", "/system/getCode","/login.html", "/css/**", "/imgs/**", "/js/**", "/plug/**")
                .permitAll()
                .antMatchers("/**")
                .hasAnyRole("超级管理员","教师","家长")
                .anyRequest()
                .authenticated()
                .and()
                .addFilterBefore(checkCodeFilter, UsernamePasswordAuthenticationFilter.class)
                .formLogin()
                .loginPage("/login.html")  //指定自定义登录页面
                .loginProcessingUrl("/login") //表单的action属性
                .successHandler(loginSuccessHandler) // 登录成功的处理器
                .failureHandler(loginFailureHandler) //登录成功失败
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")  //注销地址
                .invalidateHttpSession(true) //session自动失效
                .logoutSuccessUrl("/login.html") //注销成功跳转登录页面
                .permitAll()
                .and()
                .csrf()
                .disable();  //关闭csrf过滤器
    }
}
